import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  //获取各种环境下的对应的变量
  let env = loadEnv(mode, process.cwd())
  return {
    plugins: [
      vue(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
    ],
    resolve: {
      alias: {
        '@': path.resolve('./src'),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/styles/variable.scss";',
        },
      },
    },
    //代理跨域
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''), // 替换/api为空字符串
          bypass: (req, res, proxyOptions) => {
            const proxyUrl =
              new URL(
                proxyOptions.rewrite!(req.url) || '',
                proxyOptions.target as string,
              )?.href || ''
            console.log(proxyUrl)
          },
        },
        // '/userService': {
        //   target: 'http://127.0.0.1:8777/',
        //   changeOrigin: true,
        //   rewrite: (path) => path.replace(/^\/userService/, ''), // 替换/api2为空字符串
        //   bypass: (req, res, proxyOptions) => {
        //     const proxyUrl = new URL(proxyOptions.rewrite!(req.url) || '', (proxyOptions.target) as string)?.href || ''
        //     console.log(proxyUrl);
        //   }
        // },
      },
    },
  }
})
